'use strict';

import baseParam from './BaseParam';

export default {
    /**
     * ajaxPost请求
     * @param that
     * @param url
     * @param params
     * @param success
     */
    ajaxPost(that, url, params, success) {
        this.ajax(that, url, params, success, "post");
    },
    /**
     * ajaxGet请求
     * @param that
     * @param url
     * @param params
     * @param success
     */
    ajaxGet(that, url, params, success)  {
        this.ajax(that, url, params, success, "get");
    },
    /**
     * ajax请求
     * @param that
     * @param url
     * @param params
     * @param success
     * @param method
     */
    ajax(that, url, params, success, method) {
        that.axios({
            method: method,
            url: baseParam.BASE_HTTP + url,
            headers: baseParam.HEADER,
            params: params,
            withCredentials: true
        }).then((response) => {
            console.log(response);
            // 验证是否成功返回数据
            if(response.data.success) {
                success(response.data);
            }else {
                info.error(that, response.data.message)
            }
        }).catch((error) => {
            if (error.response) {
                // 请求已发出，但服务器响应的状态码不在 2xx 范围内
                console.log(error.response.data);
                console.log(error.response.status);
                console.log(error.response.headers);
            } else {
                console.log('Error', error.message);
            }
            console.log(error.config);
        })
    },
}
